<?php
namespace Home\Controller;
use Think\Controller;

Class MoneyController extends CommonController{

    public function _initialize() {
        parent::_initialize();
        $this->dbname = CONTROLLER_NAME;
    }
	
	public function countMoney() {
 	     $sid = 24;                       //催收人员id  钱天星
 	     $mon = '2017-10';                //统计月份
 	     $regtime = '1505962858';         //催收人员入职时间
 	     $strattime = strtotime($mon.'-'.'01');
 	     $endtime = strtotime($mon.'-'.'01 +1 month');
 	     $data = M()->query("SELECT o.order_id,o.uid,o.days,loan_time,k_time,trans_type,over_day,succ_amt 
                 	         FROM cash_order o LEFT JOIN cash_bf_dk d ON o.order_id=d.order_id 
                 	         WHERE o.collection_id=$sid AND $strattime<=k_time<$endtime AND k_time>$regtime
 	                         AND d.status='S' ORDER BY o.order_id,k_time");
 	     var_dump($data);exit;
 	     $pre = array();
 	     foreach ($data as $k=>$v) {
 	         //需要判断之前是否还有其他续期动作
 	         if ($pre['order_id'] == $v['order_id']) {
 	             $day = (($v['k_time']-$pre['k_time'])/86400);
 	             $delay = ceil($day-$pre['over_day']);
 	         } else {
 	             //处理的时候逾期的天数 距离放款或者上次续期的时间间隔
 	             $day = (($v['k_time']-$v['loan_time'])/86400);
 	             if ($day > $v['days']) {
 	                 $k_time = M()->query("SELECT k_time,over_day FROM cash_bf_dk 
 	                      WHERE order_id={$v['order_id']} AND k_time<{$v['k_time']} AND STATUS='S' 
 	                      ORDER BY k_time LIMIT 1");
 	                // var_dump($k_time);exit;
 	                 if ($k_time[0]['k_time']>0) {    //有续期记录
 	                     $day = (($v['k_time']-$k_time[0]['k_time'])/86400);
 	                     $delay = ceil($day-$k_time[0]['over_day']);
 	                 } else {                         //无续期记录
 	                     $day = (($v['k_time']-$v['loan_time'])/86400);
 	                     $delay = ceil($day-$v['days']);
 	                 }
 	             } else {
 	                 $delay = ceil($day-$v['days']);
 	             }
 	         }
 	         //var_dump($delay);exit;
 	         if ($delay<=0) {
 	             $count['<=0']++;
 	         } elseif ($delay>0 && $delay<=3) {
 	             $count['0<delay<=3']++;
 	         } elseif ($delay>3 && $delay<=7) {
 	             $count['3<delay<=7']++;
		     } elseif ($delay>7 && $delay<=10) {
 	             $count['7<delay<=10']++;
 	         } else {
 	             if ($v['trans_type'] == 3) {
 	                 if ($v['succ_amt']>=30000) {
 	                     $count['>10xu']++;
 	                 }
 	             } else {
 	                 if ($v['succ_amt']>=130000) {
 	                     $count['>10']++;
 	                 }
 	             }
 	         }
 	         //echo $delay.'|'.$v['trans_type'].'|'.$v['order_id'].'|'.$v['succ_amt'].'<br>';
 	         $pre = $v;
 	     }
 	     //var_dump($count);
 	     $money = $count['<=0'] + $count['0<delay<=3']*2 + $count['3<delay<=7']*10 
 	              + $count['7<delay<=10']*20 + $count['>10']*200 + $count['>10xu']*50;
 	     echo $money;
 	 }

}